﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Windows.Controls.Primitives;

namespace Nok_Air
{
    public partial class FlightSchedulePage : PhoneApplicationPage
    {
		private Popup popup;
        private string _oldSrc = "";
        private string _oldDest = "";
        private TextBlock tb = null;
        public FlightSchedulePage()
		{
			popup = new Popup();
			popup.Child = new ProgressBarUserControl();
            InitializeComponent();
            Nok_Air.App.ViewModel.ScheduleItems = new System.Collections.ObjectModel.ObservableCollection<NokObjects.ScheduleItemViewModel>();
           
            DataContext = App.ViewModel;

			//load airport list at startup
			popup.IsOpen = true;
			Nok_Air.WebService.NokAirServicesHelper nokSHelp = new Nok_Air.WebService.NokAirServicesHelper();
			nokSHelp.GetAvailableAirportCompleted += nokSHelp_RequestCompleted;
			nokSHelp.GetAvailableAirports();
		}

		void nokSHelp_RequestCompleted(object sender, WebService.RequestCompletedEventArgs ev)
		{
			Dispatcher.BeginInvoke(() =>
			{
		        if (ev.exception != null)
		        {
				        MessageBox.Show("Connection Error");
				        NavigationService.GoBack();
				        NavigationService.Navigated += (ss, ee) => { popup.IsOpen = false; };
			        return;
                }

                Nok_Air.App.ViewModel.ScheduleItems = ev.result as System.Collections.ObjectModel.ObservableCollection<NokObjects.ScheduleItemViewModel>;
                popup.IsOpen = false;
            });
		}

        private void airportScr2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Nok_Air.WebService.NokAirServicesHelper nokSHelp = new Nok_Air.WebService.NokAirServicesHelper();
            nokSHelp.GetDestinations(Nok_Air.App.ViewModel.AvailableAirportItemsSrc[(sender as ListPicker).SelectedIndex].AirportCode);
        }
		
        private void viewButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            string origin = Nok_Air.App.ViewModel.AvailableAirportItemsSrc[airportScr2.SelectedIndex].AirportCode;
            string destination = Nok_Air.App.ViewModel.AvailableAirportItemsDest[airportDest2.SelectedIndex].AirportCode;

            ScheduleLayout.Visibility = System.Windows.Visibility.Visible;

            if (_oldSrc == origin && _oldDest == destination)
            {
                return;
            }
            _oldSrc = origin;
            _oldDest = destination;

            DateTime dt = (DateTime)dateFlight.Value.Value;
			popup.IsOpen = true;
            Nok_Air.WebService.NokAirServicesHelper nokSHelp = new Nok_Air.WebService.NokAirServicesHelper();
			nokSHelp.GetScheduleCompleted += nokSHelp_RequestCompleted;
            nokSHelp.GetSchedule(origin, destination, dt);
        }

        private void doneButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void OK_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ScheduleLayout.Visibility = Visibility.Collapsed;
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            if (ScheduleLayout.Visibility == Visibility.Visible)
            {
                e.Cancel = true;
                ScheduleLayout.Visibility = Visibility.Collapsed;
            }
            else
            {
                e.Cancel = false;
            }
        }

        private void dateFlight_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            DateTime? value = (sender as DatePicker).Value;
            if (!value.HasValue || value.Value < DateTime.Today)
            {
                (sender as DatePicker).Value = DateTime.Today;
                MessageBox.Show(App.ViewModel.DateTimeErrorMsg);
            }
        }
    }
}